package org.itboys.mobile.controller.mobile.advertisement;




import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.util.MobileSignUtils;
import org.itboys.mobile.dto.SundayAdvertisementDto;
import org.itboys.mobile.entity.mongo.SundayAdvertisement;
import org.itboys.mobile.service.mongo.mobile.advertisement.SundayMobileAdvertisementService;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2017年2月9日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_广告（新闻，轮播图）_controller。
 * 
 */
@Controller
@RequestMapping("/sunday/mobile/advertisement")
@Api(value = "advertisement",description = "广告相关")
public class SundayMobileAdvertisementController extends BaseController {


	@Autowired
	private SundayMobileAdvertisementService advertisementService;


	/**---------------------------广告信息--------------------------**/
	/**
	 * 广告信息HTML5
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/html5" )
	public  String html5(HttpServletRequest request,
						 Model model,
						 @RequestParam(value="id",required=false) long id){
		SundayAdvertisement advertisement = advertisementService.getById(id);
		model.addAttribute("advertisement",advertisement);
		return "/sunday/advertisement/html5";
	}

	/**
	 *
	 * @param response
	 * @param request
	 * @param zoneKey
	 */

	@RequestMapping(value="/getAdvertisementByZoneKey", method= RequestMethod.POST)
	@ApiOperation(value="根据广告位获取广告信息。不分页")
	public void getAdvertisementByZoneKey(HttpServletResponse response,
						   HttpServletRequest request,
						   @ApiParam(required=true, name="zoneKey", value="广告位key")@RequestParam(value = "zoneKey",required = true) String zoneKey,
										  @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
										  @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
						   ) {
		try {
			Map<String, Object> param =new HashMap<String,Object>();
			param.put("zoneKey",zoneKey);
			param.put("nonce", nonce);
			param.put("sign", sign);
			boolean signSer = MobileSignUtils.signCheck(param, sign);
			if(!signSer){
				logger.warn("签名校验失败");
				ResponseMessage.error(-1,"签名校验失败");
				return;
			}
			List<SundayAdvertisementDto> advertisements = advertisementService.getAdvertisementByZone(zoneKey);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgResultDO(advertisements,CommonConstants.SUCCESS));
		} catch (Exception e) {
			String msg ="";
			if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
				msg=e.getMessage();
			}else{
				msg= CommonConstants.ERROMSG;
				logger.info("/sunday/mobile/advertisement/getAdvertisementByZoneKey--------"+e);
			}
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
		}
	}
}
